package com.example.mrzhou.lvmm.view.fragment.home;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.example.mrzhou.lvmm.R;
import com.example.mrzhou.lvmm.commom.base.BaseFragment;
import com.example.mrzhou.lvmm.entity.home.HomeContent;
import com.example.mrzhou.lvmm.presenter.home.HomeContentPresenter;
import com.example.mrzhou.lvmm.view.adapter.home.HomeContentAdapter;
import com.example.mrzhou.lvmm.view.impl.home.HomeContentView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeSecondFragment extends BaseFragment implements HomeContentView {

    public static final String KEY_WORD1 = "word1";
    public static final String KEY_WORD2 = "word2";

    @BindView(R.id.xclv_home_nsy)
    XRecyclerView xclvHomeNsy;
    private HomeContentAdapter adapter;
    private List<HomeContent.DatasBean.InfosBean> data = new ArrayList<>();


    public HomeSecondFragment() {
        // Required empty public constructor
    }

    public static HomeSecondFragment newInstance(String word1, String word2) {

        Bundle args = new Bundle();
        args.putString(KEY_WORD1, word1);
        args.putString(KEY_WORD2, word2);
        HomeSecondFragment fragment = new HomeSecondFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_home_second;
    }

    @Override
    protected void initView(View view) {
        ButterKnife.bind(this, view);
        xclvHomeNsy.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new HomeContentAdapter(data, getContext());
        xclvHomeNsy.setAdapter(adapter);
        xclvHomeNsy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initData();
            }

            @Override
            public void onLoadMore() {

            }
        });
        initData();
        xclvHomeNsy.setRefreshing(true);
        xclvHomeNsy.setLoadingMoreEnabled(false);
    }

    @Override
    protected void initData() {
        String backword1 = getArguments().getString(KEY_WORD1);
        String backword2 = getArguments().getString(KEY_WORD2);
        HomeContentPresenter presenter = new HomeContentPresenter(this);
        presenter.getHomeContent(backword1, backword2, System.currentTimeMillis());
    }

    @Override
    public void onSuccess(HomeContent homeContent) {
        data.clear();
        if (homeContent != null) {
            data.addAll(homeContent.getDatas().get(0).getInfos());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFaiure(Throwable a) {

    }

    @Override
    public void onFinsh() {
        xclvHomeNsy.refreshComplete();
    }

}
